﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Approval;
using MediatR;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Approval
{
    /// <summary>
    /// 请假删除
    /// </summary>
    public class DelApprovalCommandHandler : IRequestHandler<DelApprovalCommand, APIResult<object>>
    {
        //审批仓储
        private readonly IBaseRepository<LeaveApplication> leaveappRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="leaveappRep"></param>
        public DelApprovalCommandHandler(IBaseRepository<LeaveApplication> leaveappRep)
        {
            this.leaveappRep = leaveappRep;
        }

        public async Task<APIResult<object>> Handle(DelApprovalCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var entity = leaveappRep.GetAll().Result.FirstOrDefault(x => x.LeaveId == request.LeaveId);
            entity.IsDel= true;
            var r=leaveappRep.Update(entity).Result;
            if (r>0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data= r;
            }
            return await Task.FromResult(result);
        }
    }
}
